package dmt16.TelemetrySuit.UI.Tiles
{
	import dmt16.TelemetrySuit.Controller.SensorTypes.PulseSensor;
	import dmt16.TelemetrySuit.Controller.SensorTypes.SensorEvent;
	import dmt16.TelemetrySuit.UI.Assets.Fonts;
	import dmt16.TelemetrySuit.UI.Assets.HeartbeatTileGFX;
	import dmt16.TelemetrySuit.UI.PlotTools.Graph;
	import dmt16.TelemetrySuit.UI.PlotTools.GraphLine;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class PulseTile extends TileBase
	{
		public function PulseTile(sensor:PulseSensor)
		{
			super(sensor);
			_pulseSensor=sensor;
			_pulseSensor.addEventListener(SensorEvent.PULSE,_onPulse);
			_pulseSensor.addEventListener(SensorEvent.SENSOR_DATA_EVENT,_onSensorData);
			
			_graph=new Graph();
			_graph.yLim(0,250);
			
			_graphLine=new GraphLine(_pulseSensor.pulseData);
			_graphLine.lineStyle.color=0xEE1C4E;
			_graphLine.lineStyle.thickness=2;
			
			_graph.addLine(_graphLine);
			
			detailContent=_graph;
			
			_heartGFX=new HeartbeatTileGFX();
			
			_fade=Sprite(Sprite(_heartGFX.getChildAt(0)).getChildAt(1)).getChildAt(0) as Shape;
			addChild(_heartGFX);
			_heartGFX.width=WIDTH;
			_heartGFX.height=HEIGHT;
			
			_pulseText=new TextField();
			addChild(_pulseText);
			_pulseText.x=0;
			_pulseText.y=HEIGHT/3;
			_pulseText.width=WIDTH;
			_pulseText.height=HEIGHT/2;
			_pulseText.defaultTextFormat=_textFormat;
			_pulseText.embedFonts=true;
			_pulseText.selectable=false;
			addEventListener(Event.ENTER_FRAME,_onEnterFrame);
		}
		
		private function _onEnterFrame(event:Event):void{
			if(_fade.alpha>0.01){
				_fade.alpha*=0.8;
			}
			else
				_fade.alpha=0;
		}
		
		private var _pulseText:TextField;
		
		private function _onSensorData(event:SensorEvent):void{
				
		}
		
		private static var _textFormat:TextFormat;
		{
			_textFormat=new TextFormat();
			_textFormat.font=Fonts.DSDigitalBold;
			_textFormat.size=55*HEIGHT/300;
			_textFormat.align=TextFormatAlign.CENTER;
			_textFormat.color=0xFFFFFF;
		}
		private static var _textFormat2:TextFormat;
		{
			_textFormat2=new TextFormat();
			_textFormat2.font=Fonts.DSDigitalBold;
			_textFormat2.size=40*HEIGHT/300;
			_textFormat2.align=TextFormatAlign.CENTER;
			_textFormat2.color=0xFFFFFF;
		}
		
		private function _onPulse(event:Event):void{
			_fade.alpha=1;
			_pulseText.text=Math.round(_pulseSensor.pulse).toString()+"\rBPM";
			_pulseText.setTextFormat(_textFormat2,_pulseText.text.length-3,_pulseText.text.length);
		}
		
		public override function drawDetailContent():void{
			_graph.fullRedraw();
			_graph.graphics.clear();
			var h:Number=(_graph.height-120)/4;
			var M:Matrix=new Matrix();
			M.createGradientBox(_graph.width,_graph.height-120,Math.PI/2);
			_graph.graphics.beginGradientFill(GradientType.LINEAR,[0xff3333,0xff3333,0xff3333,0xff3333],[0.5,0.3,0,0],[0,255/3,255*2/3,255],M);
			_graph.graphics.drawRect(0,0,_graph.width,_graph.height-120);
		}
		
		public override function get title():String{
			return "Pulse Rate";
		}
		
		private var _heartGFX:HeartbeatTileGFX;
		private var _fade:Shape;
		
		private var _pulseSensor:PulseSensor;
		
		private var _graphLine:GraphLine;
		private var _graph:Graph;
	}
}